import { Component, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STRequestOptions, STChange, STData } from '@delon/abc/st';
import { SFDateWidgetSchema, SFSchema } from '@delon/form';
import { SearchDrawerService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BasicTableComponent } from 'src/app/routes/commom';
import { TicketService } from '../../services/ticket.service';
import { AddCollectionInvoiceModalComponent } from './add-collection-invoice-modal/add-collection-invoice-modal.component';

@Component({
  selector: 'app-input-invoice',
  templateUrl: './input-invoice.component.html',
  styleUrls: ['../../../commom/less/commom-table.less']
})
export class InputInvoiceComponent extends BasicTableComponent {
  @ViewChild('auditModal', { static: false })
  auditModal!: any;
  columns: STColumn[] = this.initST();
  schema: SFSchema = this.initSF();

  selectedRows: any[] = [];
  constructor(
    public service: TicketService,
    private nzModalService: NzModalService,
    private router: Router,
    public searchDrawerService: SearchDrawerService
  ) {
    super(searchDrawerService);
  }

  search() {
    this.st?.load(1);
  }

  beforeReq = (requestOptions: STRequestOptions) => {
    if (this.sf) {
      Object.assign(requestOptions.body, {
        ...this.sf?.value,
        createtime: {
          start: this.sf?.value.createtime?.[0] || '',
          end: this.sf?.value.createtime?.[1] || ''
        },
        invdate: {
          start: this.sf?.value.invdate?.[0] || '',
          end: this.sf?.value.invdate?.[1] || ''
        }
      });
    }
    return requestOptions;
  };

  stChange(e: STChange): void {
    switch (e.type) {
      case 'checkbox':
        this.selectedRows = e.checkbox!;
        break;
    }
  }

  addInvoice() {
    if (this.selectedRows?.length <= 0) {
      this.service.msgSrv.warning('请选择申请记录');
      return;
    }
    const modal = this.nzModalService.create({
      nzTitle: '收票信息',
      nzContent: AddCollectionInvoiceModalComponent,
      nzComponentParams: { i: { userId: 0 } },
      nzFooter: null
    });
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        inpinvcode: {
          type: 'string',
          title: '收票单号',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入'
          }
        },
        ltdid: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          },
          default: ''
        },
        invoiceno: {
          type: 'string',
          title: '发票号码',
          ui: {
            autocomplete: 'off',
            placeholder: '请输入'
          }
        },
        invtype: {
          type: 'string',
          title: '发票类型',
          enum: [
            { value: '', label: '全部' },
            { value: '运输专票', label: '运输专票' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: ''
        },
        hrto: {
          type: 'string',
          title: '销售方',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getCRMCustomerId()
          }
        },
        createtime: {
          title: '创建时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        },
        sts: {
          type: 'string',
          title: '收票状态',
          enum: [
            { value: '', label: '全部' },
            { value: '1', label: '新建' },
            { value: '2', label: '关闭' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: ''
        },
        invdate: {
          type: 'string',
          title: '发票日期',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd'
          }
        },
        remarks: {
          type: 'string',
          title: '收票备注',
          ui: {
            placeholder: '请输入'
          }
        },
        billCode: {
          type: 'string',
          title: '订单号',
          ui: {
            placeholder: '请输入'
          }
        },
        feecode: {
          type: 'string',
          title: '费用号',
          ui: {
            placeholder: '请输入'
          }
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      { title: '', index: 'key', type: 'checkbox', width: 60, className: 'text-center', fixed: 'left' },
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '收票单号', index: 'inpinvcode', type: 'link', width: 190 },
      { title: '网络货运人', index: 'ltdName', width: 220 },
      { title: '发票日期', index: 'invdate', type: 'date', width: 150, className: 'text-center' },
      { title: '发票号', index: 'invoiceno', width: 130 },
      {
        title: '发票金额',
        index: 'invmoney',
        width: 100,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.invmoney }) }
      },
      {
        title: '税额',
        index: 'invtax',
        width: 100,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.invtax }) }
      },
      { title: '发票类型', index: 'invtype', width: 150, className: 'text-center' },
      { title: '销售方', index: 'hrtoName', width: 200 },
      { title: '创建时间', index: 'createTime', type: 'date', width: 150, className: 'text-center' },
      { title: '创建人', index: 'createbyname', width: 120 },
      { title: '收票状态', index: 'stsLabel', width: 120, className: 'text-center' },
      {
        title: '操作',
        fixed: 'right',
        className: 'text-center',
        width: 120,
        buttons: [
          {
            text: '浏览',
            acl: { ability: ['TICKET-INPUT-INVOICE-view'] },
            click: item => this.router.navigate(['/ticket/input-invoice/detail/' + item.id])
          },
          {
            text: '修改',
            acl: { ability: ['TICKET-INPUT-INVOICE-edit'] },
            click: item => this.router.navigate(['/ticket/input-invoice/edit/1'])
          }
        ]
      }
    ];
  }
}
